<?php
/**
 * Created by IntelliJ IDEA.
 * User: luwei
 * Date: 2017/7/25
 * Time: 15:44
 */

namespace app\modules\api\models;

use app\hejiang\ApiCode;
use app\models\PurchaseGoods;
use app\models\Purchase;
use app\models\Model;

class PurchaseGoodsForm extends ApiModel
{
    public $store_id;
    public $model;
	public $user_id;
    public $goods_list;

    public $review_result;


    public function rules()
    {
        return [
            [['store_id','user_id'], 'integer'],
			 [['goods_list'], 'string'],
            [['user_id'], 'integer']
        ];
    }

    public function attributeLabels() 
    { 
        return [ 
            'store_id' => 'Store ID',
            'user_id' => '用户',
			'goods_list'=>'商品列表',
           
        ]; 
    }

    public function save()
    {
		$goods = new PurchaseGoods();
        if (!$this->validate()) {
            return $this->errorResponse;
        }

        $goods->store_id = $this->store_id;
       
		$model = PurchaseGoods::findOne([
						'user_id' => $this->user_id,
						'store_id' => $this->store->id,
					]);
		$purchase = Purchase::findOne([
						'user_id' => $this->user_id,
						'store_id' => $this->store->id,
					]);			
		if(!$model){
			if(!$purchase )	{
				return [
						'code' => 0,
						'msg'  => '你还不是团长,不能添加',
					];	
			}
			$goods->purchase_id =$purchase['id'];
			$goods->user_id = $this->user_id;
			$goods->create_time = t;
			$goods->goods_list = $this->goods_list;		
			if ($goods->save()) {
				return [
					'code' => ApiCode::CODE_SUCCESS,
					'msg'  => '添加成功',
					];
			} else {
				return [
					'code' => 0,
					'msg'  => '操作失败，请稍后重试',
				];
			}
		}else{
			$model->goods_list = $this->goods_list;
			if ($model->save()) {
				return [
					'code' => 1,
					'msg'  => '更新成功',
					];
			} else {
				return [
					'code' => 0,
					'msg'  => '操作失败，请稍后重试',
				];
			}	
		}			
		
    }
}
